/**
 * @开发版权 
 * @项目名称 轻量数据中台(LDMP)
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2025-01-17
 * @修订日期
 * @描述  DeptController
 */
package com.ng.sys.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.annotation.Tx;
import com.ng.common.controller.Controller;
import com.ng.common.model.response.R;
import com.ng.common.util.UuidUtil;
import com.ng.sys.model.domain.Dept;
import com.ng.sys.service.IDept;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
@Tag(name = "部门管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/dept")
public class DeptController extends Controller{

	@Autowired
	private IDept deptImpl;

	@Operation(summary = "获取部门树列表")
	@Parameters({
		 @Parameter(name = "keywords", description = "关键字", required = false),
		 @Parameter(name = "status", description = "状态(1-正常|0-禁用)", required = false)
	})
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public R<List<Dept>> list(HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "status", required = false, defaultValue = "") String status) {
		String sqlExceptSelect = "select * from " + Dept.TABLE_NAME + " a where 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " and (a.name like '%" + keywords + "%' or  a.code like '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(status)) {
			sqlExceptSelect += " and a.status='"+status+"' ";
		}
		sqlExceptSelect += " order by a.sort asc";
		return R.ok(Dept.dao.find(sqlExceptSelect));
	}

	
	@Operation(summary = "保存部门")
	@Parameters({
		 @Parameter(name = "dept", description = "部门", required = false)
	})
	@Tx
	@TokenRequired
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Dept> save(HttpServletRequest request,@RequestBody Dept dept) {
		if (StrKit.isBlank(dept.getId())) {
			Dept checkDept=Dept.dao.findFirst("select * from "+Dept.TABLE_NAME+" where code=? ",dept.getCode());
			if(checkDept!=null) {
				return R.failed("部门编号已存在.");
			}
			dept.setId(UuidUtil.getUUID());
			dept.setCreateUserId(this.getUserId(request));
			if(StrKit.isBlank(dept.getParentId())) {
				dept.setParentId("-1");
			}
			if (dept.save())
				return R.ok();
		}
		
		Dept checkDept=Dept.dao.findFirst("select * from "+Dept.TABLE_NAME+" where id!=? and code=? ",dept.getId(),dept.getCode());
		if(checkDept!=null) {
			return R.failed("部门编号已存在.");
		}
		dept.setUpdateUserId(this.getUserId(request));
		if (dept.update())
			return R.ok();
		return R.failed();
	}

	@Operation(summary = "删除部门")
	@Parameters({
		 @Parameter(name = "ids", description = "部门Id集合", required = false)
	})
	@DeleteMapping("/{ids}")
	@TokenRequired
	public R<String> deleteDept(@PathVariable("ids") String ids) {
		deptImpl.delete(ids);
		return R.ok();
	}

	@Operation(summary = "部门下拉列表")
	@GetMapping("/options")
	@TokenRequired
	public R<List<Dept>> listDeptOptions(HttpServletRequest request) {
		return R.ok(Dept.dao.find("select * from "+Dept.TABLE_NAME+" where status='1' order by sort asc"));
	}
	
}
